﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace work18
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }
        //编写数据库连接串
        static string connStr = "server=.;database=dbtest;Uid=sa;Pwd=123456;";
        //创建SQLConnection的实例
        SqlConnection conn = new SqlConnection(connStr);


        //退出按钮
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        

        //登入按钮
        private void button1_Click(object sender, EventArgs e)
        {
            Form1 form1 = new Form1();
            //打开数据库连接
            conn.Open();
            string sql = "Select count(*) from Login where admin='{0}' and password='{1}'";
            //填充SQL语句
            sql = string.Format(sql, textBox1.Text, textBox2.Text);
            //创建SqlCommand对象
            SqlCommand cmd = new SqlCommand(sql, conn);
            //执行SQL语句
            int returnvalue = (int)cmd.ExecuteScalar();
            //判断SQL语句是否执行成功
            if (returnvalue != 0)
            {
                MessageBox.Show("登录成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                form1.Show();
                this.Hide();
            }
            else
            {
                MessageBox.Show("登录失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

            }
            conn.Close();
        }


        //注册按钮
        private void button3_Click(object sender, EventArgs e)
        {
            //打开数据库连接
            conn.Open();
            string sql = "insert into Login(admin,password) values('{0}','{1}')";
            //填充SQL语句
            sql = string.Format(sql, textBox1.Text, textBox2.Text);
            //判断SQL语句是否执行成功
            if (this.textBox1.Text == "")
            {
                MessageBox.Show("用户名不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            if (this.textBox2.Text == "")
            {
                MessageBox.Show("密码不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                //创建SqlCommand对象
                SqlCommand cmd = new SqlCommand(sql, conn);
                //执行SQL语句
                cmd.ExecuteNonQuery();
                MessageBox.Show("注册成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            conn.Close();
        }

    }

}


